package com.moto.util;

import java.io.File;
import java.io.IOException;

import org.apache.log4j.Logger;

import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;

public class QiNiuFileUpload {

	private static final Logger LOG = Logger.getLogger(QiNiuFileUpload.class);

	/**
	 * 七牛密钥AccessKey
	 */
	public static final String AccessKey = "E2RM0GfXMTJB1qgv_YFnDq_fCYrW8efG2H671Cxq";

	/**
	 * 七牛密钥SecretKey
	 */
	public static final String SecretKey = "S_gihDo89iYdbQd33jayJoEYU2xkuPm8zlD7_CcM";


	private static final String NAME_SPACE = "motocycle";

	/**
	 * 7牛默认外链URL
	 */
	private static final String DEFAULT_URL = "http://7xt0fg.com1.z0.glb.clouddn.com/";

	/**
	 * 上传文件并返回外链URL
	 * 
	 * @param filePath
	 * @param fileName
	 * @return
	 * @throws IOException
	 */
	public static String upload(String filePath, String fileName) throws IOException {
		// 前一个参数是从七牛网站上得到的AccessKey,后一个参数是SecretKey
		Auth auth = Auth.create(AccessKey, SecretKey);
		// 第一个参数是你建立的空间名称，第三个是时间长度(按毫秒算的，我这里写的是10年)
		String token = auth.uploadToken(NAME_SPACE, fileName, 1000 * 3600 * 24 * 12 * 10, null);

		// 创建上传对象
		UploadManager uploadManager = new UploadManager();

		try {
			Response res = uploadManager.put(filePath, fileName, token);
			// 打印返回的信息
			LOG.info(res.bodyString());
		} catch (QiniuException e) {
			Response r = e.response;
			// 请求失败时打印的异常的信息
			LOG.info(r.toString());
			try {
				// 响应的文本信息
				LOG.info(r.bodyString());
			} catch (QiniuException e1) {
				// LOG.error(e1);
			}
		}

		return DEFAULT_URL + fileName;
	}

//	public static void main(String args[]) throws IOException {
//		String fileName = "cq.png";
//		String filePath = "D:/upload/"+fileName;
//		
//		String url = upload(filePath, fileName);
//		System.out.println(url);
//		test2();
//	}

	
	public static void test2()throws IOException{
		String filePath = "D:/upload/";
		File file = new File(filePath);
		File[] listFiles = file.listFiles();
		for (File f : listFiles) {
			String name = f.getName();
			String path = filePath+name;
			String url= upload(path, name);
			System.out.println(url);
		}
	}
	
	


	public static void test() throws IOException {
		String filePath = "D:/upload/";
		String oem_id = "18165500506";
		File file = new File("D:/upload/");
		File[] listFiles = file.listFiles();
		
		for (File f : listFiles) {
			StringBuffer  insert =new StringBuffer("insert into `motorcycle`.`moto_type`(`type_id`,`oem_id`,`type_name`,`type_icon`) values");
			insert.append("('"+CreateID.generate()+"','"+oem_id+"',");
			String name = f.getName();
			String type_name = name.substring(0, name.indexOf("."));
			insert.append("'"+type_name+"',");
			String path = filePath+name;
			String type_icon = upload(path, name);
			insert.append("'"+type_icon+"');");
			
			System.out.println(insert.toString());
		}
		
		
		
		
	}
}
